// 数组对象判空重组
export const paramsFormat = (paramsObj) => {
  const params = {};
  Object.keys(paramsObj).forEach((key) => {
    if (paramsObj[key] || paramsObj[key] === 0) {
      params[key] = paramsObj[key];
    }
  });
  return params;
};

function getMonth(timestamp) {
  return new Date(timestamp).getMonth() + 1;
}
// 获取指定时间戳年份
function getYear(timestamp) {
  return new Date(timestamp).getFullYear();
}
// 获取当前年有多少天
function getYearDay(year) {
  // var year = new Date().getFullYear()
  let day = 365;
  if (year % 4 === 0) {
    day = 366;
  }
  return day;
}

// 获取一个月有多少天
/* 1/3/5/7/8/10/12公历月 31天
  * 闰年2月29天 其它年份28天  4年一个小润年 366天
  *year 年份
  *month 月份
**/
function getMonthDay(year, month) {
  let day = 0;
  month = parseInt(month);
  const bigMonth = [1, 3, 5, 7, 8, 10, 12];
  if (bigMonth.indexOf(month) !== -1) {
    day = 31;
  } else if (month === 2) {
    if (getYearDay(year) === 365) {
      day = 28;
    }
    if (getYearDay(year) === 366) {
      day = 29;
    }
  } else {
    day = 30;
  }
  return day;
}

function getMonthTime(timestamp) {
  const year = getYear(timestamp);
  const month = getMonth(timestamp);
  const day = getMonthDay(year, month);
  return 24 * 60 * 60 * day - 1;
}

// 范围参数处理 addTime:是否需要加上当天的时间
export function paramsRangeFormat(rangeKeyAttr = [], params = {}) {
  rangeKeyAttr.forEach(i => {
    /**
     * addTime Boolean 是否加上当天的时间戳
     * monthEnd Boolean 结束时间是否为当前月的最后一天
     * dayEnd Boolean 结束时间是否为当天的最后一秒
     * */
    const { attr, start, end, type, addTime, monthEnd, isMonth, dayEnd } = i;
    if (params[attr] && params[attr].length === 2) {
      switch (type) {
        case 'number':
          params[start] = params[attr][0] || params[attr][0] === 0 ? Number(params[attr][0]) : null;
          params[end] = params[attr][1] || params[attr][1] === 0 ? Number(params[attr][1]) : null;
          break;
        case 'money':
          params[start] = params[attr][0] || params[attr][0] === 0 ? (Number(params[attr][0]) * 100) : null;
          params[end] = params[attr][1] || params[attr][1] === 0 ? (Number(params[attr][1]) * 100) : null;
          break;
        case 'moneyFixedTwo':
          params[start] = params[attr][0] || params[attr][0] === 0 ? Number(params[attr][0]).toFixed(2) : null;
          params[end] = params[attr][1] || params[attr][1] === 0 ? Number(params[attr][1]).toFixed(2) : null;
          break;
        case 'moneyFixed':
          params[start] = params[attr][0] || params[attr][0] === 0 ? (Number(params[attr][0]).toFixed(2) * 100).toFixed(0) : null;
          params[end] = params[attr][1] || params[attr][1] === 0 ? (Number(params[attr][1]).toFixed(2) * 100).toFixed(0) : null;
          break;
        case 'fansNums':
          params[start] = params[attr][0] || Number(params[attr][0]) === 0 ? Number(params[attr][0]) : null;
          params[end] = params[attr][1] || Number(params[attr][1]) === 0 ? Number(params[attr][1]) : null;
          break;
        case 'time':
          params[start] = params[attr][0] > 0 ? Math.floor(params[attr][0] / 1000) : 0;
          params[end] = params[attr][1] > 0 ? Math.floor(params[attr][1] / 1000) : 0;
          if (dayEnd) {
            const t = params[end].toString().length === 10 ? Math.floor(params[end] * 1000) : params[end];
            const t1 = Math.floor(new Date(t).setHours(0, 0, 0, 0) / 1000);
            params[end] = t1 + 86399;
          }
          if (addTime) {
            params[end] += 86399;
          }
          if (monthEnd) {
            const monthEnd = getCurrentMonthEnd(params[end]);
            if (monthEnd) params[end] = monthEnd;
          }
          break;
        case 'round':
          params[start] = Math.round(params[attr][0]);
          params[end] = Math.round(params[attr][1]);
          break;
        default:
          params[start] = params[attr][0];
          params[end] = params[attr][1];
          if (addTime) {
            const time = isMonth ? getMonthTime(params[start]) : 86399;
            params[end] += time * 1000;
          }
          break;
      }
      delete params[attr];
    }
  });
}

// 多选参数数组转为字符串
export function paramsAryToStr(paramsArr = [], params = {}) {
  if (paramsArr.length > 0) {
    paramsArr.forEach(key => {
      if (params[key] && Array.isArray(params[key])) {
        params[key] = params[key].join(',');
      }
    });
  }
}
/**
 * 获取当前月份的最后一天，最后一秒
 * @param {timestamp} timestamp 时间戳
 * @return {timestamp} 毫秒级时间戳
 */
export function getCurrentMonthEnd(timestamp) {
  if (!timestamp || typeof timestamp !== 'number') return;
  const len = timestamp.toString().length;
  if (len !== 10 && len !== 13) return;
  if (len === 10) {
    timestamp = timestamp * 1000;
  }
  const curDate = new Date(timestamp);
  const year = curDate.getFullYear();
  const month = curDate.getMonth();
  const newDate = new Date(year, month + 1).getTime() - 1;
  return len === 10 ? Math.floor(newDate / 1000) : newDate;
}
